package com.xxxt.zpc.service.impl;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xxxt.dao.CommonMapper;
import com.xxxt.entity.Admin;
import com.xxxt.entity.Student;
import com.xxxt.entity.Teacher;
import com.xxxt.zpc.service.CommonService;
@Service
public class CommonServiceImpl implements CommonService {

	@Autowired
	CommonMapper commonMapper;
	
	@Override
	public Student studentLogin(String name, String password,HttpSession session) {
		Student student=commonMapper.studentLogin(name, password);
		if(student!=null){
			session.setAttribute("identity", "student");
			session.setAttribute("account", student.getSno());
			return student;
		}
		return null;
	}

	@Override
	public Teacher teacherLogin(String name, String password,HttpSession session) {
		Teacher teacher=commonMapper.teacherLogin(name, password);
		if(teacher!=null){
			session.setAttribute("identity", "teacher");
			session.setAttribute("account", teacher.getTno());
			return teacher;
		}
		return null;
	}

	@Override
	public Admin adminLogin(String name, String password,HttpSession session) {
		Admin admin=commonMapper.adminLogin(name, password);
		if(admin!=null){
			session.setAttribute("identity", "admin");
			session.setAttribute("account", admin.getAdminId());
			return admin;
		}
		return null;
	}

	@Override
	public Boolean updatePasswordById(String identity,String password, String id) {
		if(identity.equals("student")){
			return commonMapper.updatePasswordById("xx_student",password, "sno", id);
		}else if(identity.equals("teacher")){
			return commonMapper.updatePasswordById("xx_teacher",password, "tno", id);
		}else if(identity.equals("admin")){
			return commonMapper.updatePasswordById("xx_admin",password, "adminID", id);
		}
		return false;
	}

	@Override
	public Integer selectStudentByIdAndPassword(String identity,String id, String password) {
		if(identity.equals("student")){
			return commonMapper.selectStudentByIdAndPassword("xx_student", "sno", id,password);
		}else if(identity.equals("teacher")){
			return commonMapper.selectStudentByIdAndPassword("xx_teacher", "tno", id,password);
		}else if(identity.equals("admin")){
			return commonMapper.selectStudentByIdAndPassword("xx_admin", "adminID", id,password);
		}
		
		return 0;
	}

}
